package com.egao.common.module.pepesi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.exception.ParameterException;
import com.egao.common.core.web.JsonResult;
import com.egao.common.module.pepesi.entity.PepsiIssues;
import com.egao.common.module.pepesi.entity.PepsiStudent;
import com.egao.common.module.pepesi.entity.vo.ParentPepsiVO;
import com.egao.common.module.pepesi.entity.vo.PepsiStudentVO;
import com.egao.common.module.pepesi.mapper.PepsiStudentMapper;
import com.egao.common.module.pepesi.service.PepsiIssuesService;
import com.egao.common.module.pepesi.service.PepsiStudentService;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 学生的100件事接口实现类
 *
 * @author pyx
 */
@Service
public class PepsiStudentServiceImpl extends ServiceImpl<PepsiStudentMapper, PepsiStudent> implements PepsiStudentService {
    @Autowired
    private StudentService studentService;
    @Autowired
    private PepsiIssuesService pepsiIssuesService;

    @Override
    public JsonResult pepsiStudentData(Integer userId, Integer finishState) {
        if (userId == null) {
            throw new ParameterException("参数异常");
        }

        Student student = studentService.getByUserId(userId);
        if (student == null) {
            throw new ParameterException("学生信息不存在");
        }
        Integer studentId = student.getId();

        QueryWrapper<PepsiStudent> pepsiStudentQueryWrapper = new QueryWrapper<>();
        pepsiStudentQueryWrapper.eq("student_id", studentId);

        if (baseMapper.selectCount(pepsiStudentQueryWrapper) == 0) {
            boolean save = savePepsi(studentId);

            Integer finishCount = 0;
            Integer noFinishCount = 0;
            List<ParentPepsiVO> tree = new ArrayList<>();

            if (!save) {
                throw new ParameterException("添加学生事项失败");
            }

            List<PepsiStudentVO> listPepsiStudent = baseMapper.getPepsiStudent(studentId, finishState);
            tree = tree(listPepsiStudent);
            return JsonResult.ok().setData(tree).put("finishCount", 0).put("noFinishCount", 100);
        }

        return pepsiData(studentId, finishState);
    }

    @Override
    public JsonResult pepsiData(Integer studentId, Integer finishState) {

        Integer finishCount = 0;
        Integer noFinishCount = 0;
        List<ParentPepsiVO> tree = new ArrayList<>();

        List<PepsiStudentVO> listPepsiStudent = baseMapper.getPepsiStudent(studentId, finishState);

        finishCount = finishCount(listPepsiStudent, finishState);
        noFinishCount = 100 - finishCount;

        //没有事件则清除大标题事件
        if (finishState != null && ((finishState == 0 && finishCount == 0) || (finishState == 1 && noFinishCount == 0))) {
            tree = null;
        } else {
            tree = tree(listPepsiStudent);
        }

        return JsonResult.ok().setData(tree).put("finishCount", finishCount).put("noFinishCount", noFinishCount);
    }

    @Override
    public boolean savePepsi(Integer studentId) {
        List<PepsiIssues> pepsiIssues = pepsiIssuesService.listAll();
        List<PepsiStudent> listPepsiStudent = new ArrayList<>();

        for (PepsiIssues p : pepsiIssues) {
            PepsiStudent pepsiStudent = new PepsiStudent();
            pepsiStudent.setPepsiId(p.getId());
            pepsiStudent.setStudentId(studentId);
            pepsiStudent.setFinishState(1);
            pepsiStudent.setSort(p.getSort());
            listPepsiStudent.add(pepsiStudent);
        }

        if (saveBatch(listPepsiStudent)) {
            return true;
        }
        return false;
    }

    @Override
    public JsonResult updatePepsi(PepsiStudentVO pepsiStudentVO) {
        PepsiStudent pepsiStudent = new PepsiStudent();
        List<HashMap<String, String>> listFile = pepsiStudentVO.getListFile();

        if (listFile != null) {
            switch (listFile.size()) {
                case 0:
                    //清楚所有
                    pepsiStudent.setFileNameOne("");
                    pepsiStudent.setFileUrlOne("");
                    pepsiStudent.setFileNameSecond("");
                    pepsiStudent.setFileUrlSecond("");
                    pepsiStudent.setFileNameThird("");
                    pepsiStudent.setFileUrlThird("");
                    break;
                case 1:
                    pepsiStudent.setFileNameOne(listFile.get(0).get("name"));
                    pepsiStudent.setFileUrlOne(listFile.get(0).get("url"));

                    //清空附件2,3
                    pepsiStudent.setFileNameSecond("");
                    pepsiStudent.setFileUrlSecond("");
                    pepsiStudent.setFileNameThird("");
                    pepsiStudent.setFileUrlThird("");
                    break;
                case 2:
                    pepsiStudent.setFileNameOne(listFile.get(0).get("name"));
                    pepsiStudent.setFileUrlOne(listFile.get(0).get("url"));
                    pepsiStudent.setFileNameSecond(listFile.get(1).get("name"));
                    pepsiStudent.setFileUrlSecond(listFile.get(1).get("url"));

                    //清空附件3
                    pepsiStudent.setFileNameThird("");
                    pepsiStudent.setFileUrlThird("");
                    break;
                case 3:
                    pepsiStudent.setFileNameOne(listFile.get(0).get("name"));
                    pepsiStudent.setFileUrlOne(listFile.get(0).get("url"));
                    pepsiStudent.setFileNameSecond(listFile.get(1).get("name"));
                    pepsiStudent.setFileUrlSecond(listFile.get(1).get("url"));
                    pepsiStudent.setFileNameThird(listFile.get(2).get("name"));
                    pepsiStudent.setFileUrlThird(listFile.get(2).get("url"));
                    break;
                default:
            }
        }

        pepsiStudent.setId(pepsiStudentVO.getId());
        pepsiStudent.setFinishState(pepsiStudentVO.getFinishState());
        pepsiStudent.setRemarks(pepsiStudentVO.getRemarks());
        pepsiStudent.setChangeTime(pepsiStudentVO.getUpdateTime());

        baseMapper.updateById(pepsiStudent);
        return JsonResult.ok("更新成功");
    }

    @Override
    public PepsiStudentVO getId(Integer id) {
        if (id == null) {
            throw new ParameterException("参数异常");
        }

        PepsiStudent pepsiStudent = baseMapper.selectById(id);
        if (pepsiStudent == null) {
            throw new ParameterException("没有获取到相关信息");
        }

        PepsiStudentVO pepsiStudentVO = new PepsiStudentVO();
        BeanUtil.copyProperties(pepsiStudent, pepsiStudentVO);
        List<HashMap<String, String>> listFile = new ArrayList<>();

        //将附件封装到vo对象的list中
        if (!StrUtil.isBlank(pepsiStudent.getFileNameOne()) && !StrUtil.isBlank(pepsiStudent.getFileUrlOne())) {
            HashMap<String, String> map = new HashMap<>();
            map.put("name", pepsiStudent.getFileNameOne());
            map.put("url", pepsiStudent.getFileUrlOne());
            listFile.add(map);
        }
        if (!StrUtil.isBlank(pepsiStudent.getFileNameSecond()) && !StrUtil.isBlank(pepsiStudent.getFileUrlSecond())) {
            HashMap<String, String> map = new HashMap<>();
            map.put("name", pepsiStudent.getFileNameSecond());
            map.put("url", pepsiStudent.getFileUrlSecond());
            listFile.add(map);
        }
        if (!StrUtil.isBlank(pepsiStudent.getFileNameThird()) && !StrUtil.isBlank(pepsiStudent.getFileUrlThird())) {
            HashMap<String, String> map = new HashMap<>();
            map.put("name", pepsiStudent.getFileNameThird());
            map.put("url", pepsiStudent.getFileUrlThird());
            listFile.add(map);
        }

        pepsiStudentVO.setListFile(listFile);

        return pepsiStudentVO;
    }


    //将集合处理为树形结构
    private List<ParentPepsiVO> tree(List<PepsiStudentVO> listPepsiStudent) {
        // 构建树形对象
        List<ParentPepsiVO> tree = new ArrayList<>();

        // 一级树
        for (PepsiStudentVO one : listPepsiStudent) {
            if (one.getParentPepsiId() == 0) {
                ParentPepsiVO parentPepsiVO = new ParentPepsiVO();
                parentPepsiVO.setId(one.getId());
                parentPepsiVO.setPepsiId(one.getPepsiId());
                parentPepsiVO.setParentId(one.getParentPepsiId());
                parentPepsiVO.setTitle(one.getTitle());
                tree.add(parentPepsiVO);
            }
        }

        // 二级树
        for (ParentPepsiVO t : tree) {
            ArrayList<PepsiStudentVO> childList = new ArrayList<>();
            for (PepsiStudentVO p : listPepsiStudent) {
                if (p.getParentPepsiId() != 0 && p.getParentPepsiId().equals(t.getPepsiId())) {

                    List<HashMap<String, String>> listFile = new ArrayList<>();
                    if (!StrUtil.isBlank(p.getFileNameOne()) && !StrUtil.isBlank(p.getFileUrlOne())) {
                        HashMap<String, String> map = new HashMap<>();
                        map.put("name", p.getFileNameOne());
                        map.put("url", p.getFileUrlOne());
                        listFile.add(map);
                    }
                    if (!StrUtil.isBlank(p.getFileNameSecond()) && !StrUtil.isBlank(p.getFileUrlSecond())) {
                        HashMap<String, String> map = new HashMap<>();
                        map.put("name", p.getFileNameSecond());
                        map.put("url", p.getFileUrlSecond());
                        listFile.add(map);
                    }
                    p.setListFile(listFile);

                    childList.add(p);
                }
            }

            t.setListPepsi(childList);

            if (t.getListPepsi().size() == 0) {
                t.setListPepsi(null);
            }
            //把查询到的子类数据移除
            listPepsiStudent.removeAll(childList);
        }

        return tree;
    }

    //处理完成与未完成数
    private int finishCount(List<PepsiStudentVO> list, Integer finishState) {
        int finishCount = 0;
        if (finishState == null) {
            for (PepsiStudentVO p : list) {
                if (0 == p.getFinishState() && !(0 == p.getParentPepsiId())) {
                    finishCount++;
                }
            }
            return finishCount;
        } else if (finishState == 0) {
            for (PepsiStudentVO p : list) {
                if (!(0 == p.getParentPepsiId())) {
                    finishCount++;
                }
            }
            return finishCount;
        } else {
            for (PepsiStudentVO p : list) {
                if (!(0 == p.getParentPepsiId())) {
                    finishCount++;
                }
            }
            return 100 - finishCount;
        }
    }

    @Override
    public Integer countStudent(Integer year, Integer degree) {
        return baseMapper.countStudent(year, degree);
    }

    @Override
    public Integer countPepsi(Integer year, Integer degree) {
        return baseMapper.countPepsi(year, degree);
    }
}
